using System.Html;
using google.maps;

namespace GMapsSharp
{
    public class MapWrapper
    {
        #region [ Properties ]

        private Map _map;
        public Map Map
        {
            get { return _map; }
            set { _map = value; }
        }

        private Geocoder _geocoder;
        public Geocoder Geocoder
        {
            get { return _geocoder; }
            set { _geocoder = value; }
        }

        private DirectionsService _directionsService;
        public DirectionsService DirectionsService
        {
            get { return _directionsService; }
            set { _directionsService = value; }
        }

        private DirectionsRenderer _directionsRenderer;
        public DirectionsRenderer DirectionsRenderer
        {
            get { return _directionsRenderer; }
            set { _directionsRenderer = value; }
        }

        private InfoWindow _infoWindow;
        public InfoWindow InfoWindow
        {
            get { return _infoWindow; }
            set { _infoWindow = value; }
        }

        private string _mapId;
        public string MapId
        {
            get { return _mapId; }
            set { _mapId = value; }
        }

        #endregion

        public MapWrapper(string mapId)
        {
            MapId = mapId;
        }

        private void InitializeMap()
        {
            MapOptions mo = new MapOptions();
            mo.Zoom = 10;
            mo.Center = new LatLng(38.084724, 23.610018);
            mo.MapTypeId = MapTypeId.ROADMAP;
            mo.DisableDoubleClickZoom = true;
            mo.Draggable = true;
            mo.StreetViewControl = false;
            mo.MapTypeControl = false;
            mo.PanControl = false;
            TravelMode tm = TravelMode.BICYCLING;
            Map = new Map(Document.GetElementById(MapId), mo);

            GEvent.AddListener(Map, "click",
                delegate
                {
                    if (InfoWindow != null)
                    {
                        InfoWindow.Close();
                        InfoWindow = null;
                    }
                });
            Distance d = new Distance();
            d.Text = "testing json object";
            Duration duration = new Duration();
            duration.Text = "testing for duration JSON Object";

            DistanceMatrixRequest matrixRequest = new DistanceMatrixRequest();
            matrixRequest.AvoidHighways = true;
            matrixRequest.TravelMode = TravelMode.DRIVING;

            DistanceMatrixService matrixService = new DistanceMatrixService();
            matrixService.GetDistanceMatrix(matrixRequest,
                delegate(DistanceMatrixResponse dr, DistanceMatrixStatus status)
                    {
                        if (status == DistanceMatrixStatus.OK)
                        {
                            string txt = dr.Rows[0].Elements[0].Distance.Text;
                        }
                    });
        }

        public void Initialize(string mapId, bool dragDirections)
        {
            InitializeMap();
            Geocoder = new Geocoder();
            DirectionsService = new DirectionsService();
            InfoWindow = new InfoWindow();
            InfoWindow.GetContentElement();
            DirectionsRendererOptions dro = new DirectionsRendererOptions();
            dro.Draggable = true;
            DirectionsRenderer = new DirectionsRenderer(dro);
            DirectionsRenderer.SetMap(Map);
        }
    }
}
